package com.changpay.zywy.ui.activity.login

import android.Manifest
import android.content.Intent
import android.view.View
import android.widget.Toast
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.help.PermissionHelper
import com.changpay.zywy.ui.activity.web.WebViewActivity
import com.just.agentweb.ActionActivity.REQUEST_CODE
import com.uuzuche.lib_zxing.activity.CaptureActivity
import com.uuzuche.lib_zxing.activity.CodeUtils
import kotlinx.android.synthetic.main.activity_register_type.*
import kotlinx.android.synthetic.main.title_layout.*
import org.jetbrains.anko.sdk27.coroutines.onClick

class RegisterTypeActivity : BaseActivity() {
    override fun attachLayoutRes(): Int {
       return R.layout.activity_register_type
    }

    override fun initData() {

    }

    override fun initView() {
        iv_back.onClick { finish() }
        tv_title.text = "邀请注册"
        rl_register_scan.setOnClickListener(onClickListener)
        rl_register_invite.setOnClickListener(onClickListener)
    }

    override fun start() {

    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.rl_register_scan -> {
                PermissionHelper.instance.getPermission(this, object : PermissionHelper.PermissionCallBack {
                    override fun onSuccess() {
                        val intent = Intent(this@RegisterTypeActivity, CaptureActivity::class.java)
                        startActivityForResult(intent, REQUEST_CODE)
                    }
                    override fun onFail() {

                    }
                }, Manifest.permission.CAMERA)
            }
            R.id.rl_register_invite -> {
                val intent = Intent(this, RegisterActivity::class.java)
                startActivity(intent)
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                val bundle = data.extras ?: return
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    val result = bundle.getString(CodeUtils.RESULT_STRING)
                    //                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                    WebViewActivity.startActivity(this, result!! + "&client=true")
                    //                    Intent intent = new Intent(mContext, RegisterActivity.class);
                    //                    intent.putExtra("inviter_id", result);
                    //                    startActivity(intent);
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(this, "扫描二维码失败", Toast.LENGTH_LONG).show()
                }
            }
        }
    }
}
